<?php

defined( 'IN_CLASS' ) or die( 'Access Error!' );

class ModelComment extends base {
	
	public function getNew() {
		$sql = "select t1.id, t1.brief, t2.author, t2.content, t2.time 
				from $this->tpix as t1, $this->tcomment as t2 where t1.id = t2.target order by t2.time desc limit 10";
		$aC = model::dbuser()->get_all( $sql );
		return $aC;
	}
	
	public function getbypid( $pid ) { //根据图片ID来获得此图片的所有评论信息
		$sql = "SELECT t1.*, t2.name FROM $this->tcomment t1, $this->tmember t2
				WHERE t1.target='{$pid}' AND t1.author=t2.id ORDER BY t1.id DESC";
		return model::dbuser()->get_all( $sql );
	}
	
	public function add( $uid, $pid, $content ) { //添加相片评论
		$sql = "INSERT INTO $this->tcomment SET target={$pid},author='{$uid}',content='{$content}',time=NOW()";
		model::dbuser()->exe( $sql );
		return model::dbuser()->update_rows();
	}
	
	public function del( $cid ) { //删除相片评论
		$sql = "DELETE FROM $this->tcomment WHERE id='{$cid}' LIMIT 1";
		model::dbuser()->exe( $sql );
		return model::dbuser()->update_rows();
	}
	
	public function get( $cid ) { //获得一条评论
		$sql = "SELECT * FROM $this->tcomment WHERE id='{$cid}'";
		return model::dbuser()->get_one( $sql );
	}
}